function getFormJson(frm) {
    var o = {};
    var a = $(frm).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return JSON.stringify(o);
}

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

function ajaxLoad(ajax_url, ajax_data, ajax_data_type, callback){
	$.ajax({
		url: ajax_url,
		type: "POST",
		data: ajax_data,	//名:值
		dataType: ajax_data_type,
		beforeSend: function(xhr, settings) {
			var csrftoken = getCookie('csrftoken');
			if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
				xhr.setRequestHeader("X-CSRFToken", csrftoken);
			}
		},
	}).done(function(msg){
	//	data = JSON.parse(msg)
		callback(msg);
	});
}

var ajaxSlow_log = false;
function ajaxSlow(ajax_url, ajax_data, ajax_data_type, callback, interval) {
	// 一般用于 message 等不需要精准处理的地方
	var rec_life = 0;		// 0: 正常; 1: 正请求; 2: 出错
	var error_count = 0;
	var threshold = 2;
	var request_count = 0; 
	function rec(){
		if (request_count != 0) {
			if (ajaxSlow_log) {
				console.log("上一个请求未结束");
			}
			return; 
		}
		if (rec_life == 1) {
			if (ajaxSlow_log) {
				console.log("跳过等待");
			}
			return;
		}
		if (rec_life == 2) {
			error_count += Math.random();
			if (error_count > threshold) {
				error_count = 0;
				threshold += 0.5;
			}
			else {
				if (ajaxSlow_log) {
					console.log("跳过: " + error_count);
				}
				return;
			}
		}
		rec_life = 1;
		data = ajax_data()	// 注意：由于 data 可能改变，要传入 data 的生成函数
		request_count += 1; 
		$.ajax({
			url: ajax_url,
			type: "POST",
			data: data,
			dataType: ajax_data_type,
			beforeSend: function(xhr, settings) {
				var csrftoken = getCookie('csrftoken');
				if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
					xhr.setRequestHeader("X-CSRFToken", csrftoken);
				}
			},
		}).done(function(msg){
			callback(msg);
			rec_life = 0; 
			request_count -= 1; 
		}).fail(function(jqXHR, textStatus){
			if (ajaxSlow_log) {
				console.log("似乎与服务器通讯不畅...");
			}
			rec_life = 2;
			request_count -= 1; 
		});
	}
	rec();
	setInterval(rec, interval);
}

function redirect(dst){
	setTimeout("window.location.reload("+dst+");", 3000);
}
